/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eac.ucm;
import eac.view.*;
import eac.model.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import medium.MsgSender;

/**
 *
 * @author Vitamin
 */

public class DeleteValue {

    private ComponentModel component;
    private ValueModel value;
    private JTable achievement;
    private JTable course;
    private DefaultTableModel model;
    private int selectedRow;
    private int selectedIndex;
    private JTextArea response;
    private String data[]=new String[10];
    private String id;
    private String sql;
    private static DeleteValue local;

    public static DeleteValue getInstance()
    {
        if(local==null)
        {
            local=new DeleteValue();
            local.component=EACenter.getInstance().getComponentModel();
            local.value=EACenter.getInstance().getValueModel();
        }
        return local;
    }


    public void deleteStudentValue()
    {
        id=value.getId();
        achievement=component.getAchievement();
        model=(DefaultTableModel)achievement.getModel();
        selectedRow=achievement.getSelectedRowCount();
        response=component.getResponse();

        if(selectedRow==1)
        {
            selectedIndex=achievement.getSelectedRow();

            data[0]=(String)model.getValueAt(selectedIndex, 0);
            data[1]=(String)model.getValueAt(selectedIndex, 1);
            data[2]=(String)model.getValueAt(selectedIndex, 2);
            data[3]=(String)model.getValueAt(selectedIndex, 3);
            data[4]=(String)model.getValueAt(selectedIndex, 4);

            sql= id+";"+data[0]+";"+data[1]+";"+data[2]+";"+data[3]+";"+data[4];
            MsgSender.getInstance().toDeleteAchievement(sql);
        }
        
    }

    public void stResponse()
    {
        achievement=component.getAchievement();
        model=(DefaultTableModel)achievement.getModel();
        selectedRow=achievement.getSelectedRowCount();
        selectedIndex=achievement.getSelectedRow();
        response=component.getResponse();

        model.removeRow(selectedIndex);
        response.setText(response.getText()+"\n> 1 record is deleted");
    }

    public void tResponse()
    {
        course=component.getCourse();
        model=(DefaultTableModel)course.getModel();
        selectedRow=course.getSelectedRowCount();
        response=component.getTresponse();
        selectedIndex=course.getSelectedRow();

        model.removeRow(selectedIndex);
        response.setText(response.getText()+"\n> 1 record is deleted");
    }


    public void deleteTeacherValue()
    {
        id=value.getTid();
        System.out.println("tid: "+id);
        course=component.getCourse();
        model=(DefaultTableModel)course.getModel();
        selectedRow=course.getSelectedRowCount();
        response=component.getTresponse();
        if(selectedRow==1)
        {
            selectedIndex=course.getSelectedRow();
            data[0]=(String)model.getValueAt(selectedIndex, 0);
            data[1]=(String)model.getValueAt(selectedIndex, 1);
            data[2]=(String)model.getValueAt(selectedIndex, 2);
            data[3]=(String)model.getValueAt(selectedIndex, 3);
            data[4]=(String)model.getValueAt(selectedIndex, 4);
            sql=data[0];
            MsgSender.getInstance().toDeleteCourse(sql);

        }

    }

}
